<?php

namespace app\api\controller\admin;

use AlibabaCloud\SDK\Iot\V20180120\Iot;
use AlibabaCloud\SDK\Iot\V20180120\Models\PubRequest;
use app\admin\model\Cmdlog;
use app\admin\model\Devicelock;
use app\admin\model\Linedevice;
use app\admin\model\Org;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdaytrade;
use app\admin\model\Shopdevice;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shoproomset;
use app\admin\model\Shoptableorder;
use app\admin\model\User;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\api\controller\Exception;
use App\Api\PrinterService;
use App\Api\PrintService;
use app\api\service\BalanceService;
use app\api\service\DeviceService;
use app\api\service\LockService;
use app\api\service\MpService;
use app\api\service\MtService;
use app\api\service\MtUtil;
use app\api\service\RoomService;
use app\api\service\VbillService;
use app\api\service\XhtMtService;
use app\common\controller\Api;
use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;
use Darabonba\OpenApi\Models\Config;
use fast\Http;
use think\exception\HttpResponseException;
use think\Response;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];



    public function tradeDay()
    {

        $type = $this->request->param('type');
        $tradyDayModel = new Shopdaytrade();
        $date = array();
        for ($i=0; $i<7; $i++){
            $date[] = date("Y-m-d" ,strtotime( "+" . $i-7 .' days', time()));
        }


        $data = array();
        $x = array();
        $y = array();
        foreach ($date as $key=>$val){

            $start = $val . " 00:00:00";
            $end = $val . " 23:59:59";

            $start = strtotime($start);
            $end = strtotime($end);
            $where = array();
            $where['create_time'] = array("between", array($start, $end));
            if($type == 'amount'){
                $amount = $tradyDayModel->where($where)->sum('total_amount');
            }else{
                $amount = $tradyDayModel->where($where)->sum('profit');
            }
            $x[] = date("m-d",$start);
            $y[] = $amount;

        }

        $data['x'] = $x;
        $data['y'] = $y;


        $this->success('',$data);


    }


    public function regUser()
    {
        $userModel = new User();
        $date = array();
        for ($i=0; $i<7; $i++){
            $date[] = date("Y-m-d" ,strtotime( "+" . $i-7 .' days', time()));
        }


        $data = array();
        $x = array();
        $y = array();
        $x[] = '日期';
        $y[] = "新用户";
        foreach ($date as $key=>$val){

            $start = $val . " 00:00:00";
            $end = $val . " 23:59:59";

            $start = strtotime($start);
            $end = strtotime($end);
            $where = array();
            $where['createtime'] = array("between", array($start, $end));
            $total = $userModel->where($where)->count('id');
            $x[] = date("m-d",$start);
            $y[] = $total;

        }

        $data['x'] = $x;
        $data['y'] = $y;


        $this->success('',$data);



    }


}

